package com.lixinlei.meetingfilm.cinema.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lixinlei.meetingfilm.cinema.controller.vo.CinemaSavedReqVO;
import com.lixinlei.meetingfilm.cinema.controller.vo.DescribeCinemasRespVO;
import com.lixinlei.meetingfilm.cinema.dao.entity.MoocCinemaT;
import com.lixinlei.meetingfilm.cinema.dao.mapper.MoocCinemaTMapper;
import com.lixinlei.meetingfilm.utils.exception.CommonServiceException;
import com.lixinlei.meetingfilm.utils.util.ToolUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CinemaServiceImpl implements CinemaServiceAPI {

    @Resource
    private MoocCinemaTMapper cinemaTMapper;

    @Override
    public void saveCinema(CinemaSavedReqVO reqVO) throws CommonServiceException {

        MoocCinemaT cinema = new MoocCinemaT();
        cinema.setCinemaName(reqVO.getCinemaName());
        cinema.setCinemaPhone(reqVO.getCinemaTele());
        cinema.setBrandId(ToolUtils.str2Int(reqVO.getBrandId()));
        cinema.setAreaId(ToolUtils.str2Int(reqVO.getAreaId()));
        cinema.setHallIds(reqVO.getHallTypeIds());
        cinema.setImgAddress(reqVO.getCinemaImgAddress());
        cinema.setCinemaAddress(reqVO.getCinemaAddress());
        cinema.setMinimumPrice(ToolUtils.str2Int(reqVO.getCinemaPrice()));

        cinemaTMapper.insert(cinema);
    }

    @Override
    public IPage<DescribeCinemasRespVO> describeCinemas(int nowPage, int pageSize) throws CommonServiceException {

        Page<MoocCinemaT> page = new Page<>(nowPage, pageSize);
        IPage<MoocCinemaT> moocCinemaTIPage = cinemaTMapper.selectPage(page, null);

        IPage<DescribeCinemasRespVO> result = new Page<>();
        result.setCurrent(moocCinemaTIPage.getCurrent());
        result.setPages(moocCinemaTIPage.getPages());
        result.setSize(moocCinemaTIPage.getSize());
        result.setTotal(moocCinemaTIPage.getTotal());

        List<DescribeCinemasRespVO> vos = new ArrayList<DescribeCinemasRespVO>(moocCinemaTIPage.getRecords().size());
        moocCinemaTIPage.getRecords().stream().forEach(c -> {
            DescribeCinemasRespVO v = new DescribeCinemasRespVO();
            v.setAreaId(c.getAreaId().toString());
            v.setBrandId(c.getBrandId().toString());
            v.setCinemaAddress(c.getCinemaAddress());
            v.setCinemaImgAddress(c.getImgAddress());
            v.setCinemaName(c.getCinemaName());
            v.setCinemaPrice(c.getMinimumPrice().toString());
            v.setCinemaTele(c.getCinemaPhone());
            v.setHallTypeIds(c.getHallIds());
            vos.add(v);
        });
        result.setRecords(vos);

        return result;
    }

}
